<?php

namespace App\Lib\Taobao;

use App\Lib\Taobao\request\GuangguangContentArticlePublishRequest;
use App\Lib\Taobao\request\GuangguangContentDeleteRequest;
use App\Lib\Taobao\request\GuangguangContentItemSearchRequest;
use App\Lib\Taobao\request\GuangguangContentItemSourceSearchRequest;
use App\Lib\Taobao\request\GuangguangContentResourceCheckRequest;
use App\Lib\Taobao\request\GuangguangContentResourceUploadRequest;
use App\Lib\Taobao\request\GuangguangContentBrandSearchRequest;
use App\Lib\Taobao\request\GuangguangContentImageUploadRequest;
use App\Lib\Taobao\request\GuangguangContentTopicGroupListRequest;
use App\Lib\Taobao\request\GuangguangContentTopicListRequest;
use App\Lib\Taobao\request\GuangguangContentTopicSearchRequest;
use App\Lib\Taobao\request\GuangguangContentVideoPublishRequest;
use TopClient;
use App\Lib\Taobao\domain\ArticlePublishDto;
use App\Lib\Taobao\domain\VideoPublishDto;

class Top
{
    public $client;

    public $sessionKey;

    public function __construct($sessionKey){

        $this->client = new TopClient(env('TAO_BAO_APP_KEY'),env('TAO_BAO_APP_SECRET'));
        $this->client->format = 'json';

        $this->sessionKey = $sessionKey;
    }

    public function uploadContent($resourceUrls){
        $req = new GuangguangContentResourceUploadRequest();
        $resourceUrls = implode(',',$resourceUrls);
        log_daily('picture_guangguang')->info($resourceUrls);

        $req->setResourceUrls($resourceUrls);
//        $req->setRelationId('');

        $res = $this->client->execute($req,$this->sessionKey);
        log_daily('picture_guangguang')->info(json_encode($res));
        return $res;
    }

    public function checkContent($dto){
        $req = new GuangguangContentResourceCheckRequest();
        log_daily('picture_guangguang')->info(json_encode($dto));

        $req->setRequestDto(json_encode($dto));
        $res = $this->client->execute($req,$this->sessionKey);
        log_daily('picture_guangguang')->info(json_encode($res));
        return $res;
    }

    public function searchBrand($keyWords,$size,$cursor){

        $req = new GuangguangContentBrandSearchRequest();

        $req->setSize($size);
        $req->setKeyword($keyWords);
        $req->setCursor($cursor);

        $res = $this->client->execute($req,$this->sessionKey);
        return $res;
    }

    public function uploadImage($type,$byte){

        $req = new GuangguangContentImageUploadRequest();
//        $req->setRelationId('69a95a4c23bada92f4a3ff1f7a6855c0');
        $req->setImageBytes($byte);
        $req->setMimeType($type);

        $res = $this->client->execute($req,$this->sessionKey);
        return $res;
    }

    public function topicGroup(){

        $req = new GuangguangContentTopicGroupListRequest();
        $res = $this->client->execute($req,$this->sessionKey);
        return $res;
    }

    public function topicList($groupId){

        $req = new GuangguangContentTopicListRequest();
        $req->setGroupId($groupId);
        $req->setSize(100);

        $res = $this->client->execute($req,$this->sessionKey);
        return $res;
    }

    public function searchTopic($key){

        $req = new GuangguangContentTopicSearchRequest();

        $req->setKeyword($key);
        $req->setSize(100);

        $res = $this->client->execute($req,$this->sessionKey);
        return $res;
    }

    public function releasePicture( ArticlePublishDto $dto){
        log_daily('picture_guangguang')->info(json_encode($dto));
        $req = new GuangguangContentArticlePublishRequest();

        $req->setRequestDto(json_encode($dto));
        $res = $this->client->execute($req,$this->sessionKey);
        log_daily('picture_guangguang')->info(json_encode($res));

        return $res;
    }

    public function releaseVideo(VideoPublishDto $dto){
        log_daily('picture_guangguang')->info(json_encode($dto));

        $req = new GuangguangContentVideoPublishRequest();

        $req->setRequestDto(json_encode($dto));
        $res = $this->client->execute($req,$this->sessionKey);
        log_daily('picture_guangguang')->info(json_encode($res));
        return $res;
    }

    public function delContent($contentId){

        $req = new GuangguangContentDeleteRequest();
        $req->setContentId($contentId);
        $res = $this->client->execute($req,$this->sessionKey);

        return $res;

    }

    public function getSource(){

        $req = new GuangguangContentItemSourceSearchRequest();
        $res = $this->client->execute($req,$this->sessionKey);

        return $res;
    }

    public function searchGoods($keywords,$cursor=1){

        log_daily('picture_guangguang')->info($keywords);
        $req = new GuangguangContentItemSearchRequest();
        $req->setSourceCode('recruit');
        $req->setCursor($cursor);
        $req->setSize(20);

        if($keywords){
            $req->setKeyword($keywords);
        }

        $res = $this->client->execute($req,$this->sessionKey);
        log_daily('picture_guangguang')->info(json_encode($res));

        return $res;
    }
}
